package com.pl.grainmall.order.config;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.pl.grainmall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118689547";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCqouMeSyRY93VwOzUMkgKPEXLT4begSI3VVNecoDmfUzCEoTEAyfKOPgZ4/dFH1h+Wx85KjEnc5DELILHo0c2yWrBxuHOg45vhsRLsv+4dkzMLWYRdgB20zfNbuRH7E3GZ1zoof+U22sWTf9LLMAJtBVLwka4SOjDtuUrLaepWwY9yUX4V5zBb61R+fCMeaj4rv4m3lIBNlAF4z5nw6GTjWAkB23yEwuWA7KG/h5QYL8nsAjLxl2uNU5+Q+iCntXqfrZJq9zy2oeu/hB3zsno4s+DH2Oanrw/e1tevD/sNoNc+oyuYQX/sWi+Tp6w9HJFJB+JFR0kabib5sXAmEm4VAgMBAAECggEAf+kAhYngYkyFnS7U0mp1Ppfe0tPF2YHpyDwEMGWvLC6pFRLb7/JyP4xhP7tMcSQio919V0aYa9wmo2fL1XHIVuVClMym6LkV9fXN8Oji7wqT3fMplQlfu1QTE/PEdciahK7g6tEMmrUeHDZaZnPDrcOq66jYqtMyMFGQrrDjuEnA/vyD46Fgv/xpvRsjvI3mcEP9YEEJxASI0zWcQhmFH8lXafONorfIrjASo+N/LPtl5cF5HRLsprnxmoZqkKOrPEb7f2Zh8W9k0C248WE1VSzBvvJ1ncNRuHSESQ04T1OpwAMuKsPVB8sMZ4M5xGcZ9oS88+pe0RAx1zMB96k8tQKBgQDU0A9MymKC5WxM8Rg/tfVLnEtVpydMSAP6ZW3XYJDyyxOMK9hoGxUrViVd3c97y44L7VblnjeVXuGp3IVXbcNQFr5W+7uswnn5g+52941Rx2C0mX7+5KXxEhd2NHgixa0Iic6e2fxQ+97LBOrBXx38Vpo2gKw/oQiOVB/9FwHLvwKBgQDNQ7KIFqAZ3CALjTgHPYfXsmbZeE4tSfHdW5/rXgqGcoJInX0Y2eUc2P+cyAM7VI4CQK9nXgksmNRaE5EkzHZD5vnac22aAwrExObRZDKBOwqm2A0Pq++IKBmgPgHy8rITYN8LNCSIS0hxqQeANJoIs1cwNlAUEjLPftnxm/wLKwKBgQDQFAsRq0T89XOaG5X5HWGVJ2GlgwVymWuCVdA7aYjwTE11nZ0/1VMyh02L5Qgs48+kq9BuPO/eFeKRojOiU4lH8qTzCbnr5k86/5uXKN89mbNdgkPg2BHEj0rvYcghP6S7F3s2aK9DQ7IIUWtz3RvFkavBJLjGkh56g5ivbuJrowKBgFwJwOG4k21FALFtTvQgKOsUuX1FrngsMeE5lklpT3rdmYGJ7G/aNBttSaA5JwnRbiOgcYX96ksu8/i30VsYuubTiLDlFP374zY7u+uj6+l6WdpaAfFG/Q6mPGdXpnQTUrnRs/FIHiFm8ELWseBQtci08aFSmTGGv3W+wWbOtIMlAoGAfv/lQLdifUPN+PgPRZkZDHddAbXHkGMbS15JydMa1z0aF+Wqeog7KhCJWYZAdRhVO1f+A2eROzcOpZCrqYjhI1oQ5pQwmT2If+NRM78CuHzRZ7H4/FC8cye5fnC1WCiak04+tAwFH88VgGbN1etmFzsuJ8I8ehV+BR6/yJP8CPU=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjDXzROqODCgQ7YKcWJs24wBsr6la/SanBgy56b1us3ep4GQBjvh4hgKXxhd4q/wPpUTEK8Kz/gGDLxspTAHN/SnroFqOEtu3Usy/yZFt6pqqwjpWbYpwVIMQMGR8kiWlv0KAUI2ImGu9nBURWhJvh8on1t4JSw7wuJLBNxuK3xl8Die+eG16KX8NIDnac3op2XwgNidDI4qapsXlCnlgypwbLqx1RskE+tpSz+al/P/TiIAux6KN1PSdCEH+V+cQaR5bt7Dl4/98T4gjKEGs+IpxyDLVfL7ndUxG2a1NG9SY3C5Yjqs713clF5bRybo6eXH4dXDBKKMnQ2CF8vkcNQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout="30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }


}
